import java.io.*;
import java.util.Scanner;

public class Demo5 {
    //进行普通文件的复制
    public static void main(String[] args) {
        System.out.println("请输入源文件的路径");
        Scanner scanner = new Scanner(System.in);
        String srcPath = scanner.next();

        System.out.println("请输入要复制的目标文件的路径");
        String desPath = scanner.next();

        //验证输入是否合法
        File srcFile = new File(srcPath),
                desFile = new File(desPath),
                desParent = desFile.getParentFile();
        if (!srcFile.isFile() || !desParent.isDirectory()) {
            System.out.println("输入的文件路径有误");
            return;
        }

        copy(srcFile,desFile);

    }

    private static void copy(File srcFile, File desFile) {

        try (InputStream src = new FileInputStream(srcFile);
                OutputStream des = new FileOutputStream(desFile)) {
            while (true) {
                byte[] bytes = new byte[1024];
                int i = src.read(bytes);
                //读完跳出循环结束
                if (i == -1) break;
                for (int j = 0; j < i; j++) {
                    des.write(bytes,0,i);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
